// Created by liusc on 2020/7/28--11:17.
import sortTree from '@/components/sort-tree'
import {mapGetters} from "vuex"
import {fetchList, addObj, putObj, getObj, delObj, initParameter,getUUID} from "@/views/wy-budget/api/budgetadjust.js";
import {getDict} from "~/utils";
import {mapState} from 'vuex'

export default {
    name: 'resource-manage',
		provide () {
			return {
				initList: this.getListData
			};
		},
    components: {sortTree},
    data() {
        return {
            fileId: '',
            fileId2: '',
            tableData: [],
            // searchForm: {},
            lists:[],
            lists2:[],
            lists3:[],
            get_button: false,
            add_button: false,
            edit_button: false,
            del_button: false,
            // placeholder: '请输入预算编号或者申请名称',
						// 请求参数
						params: {
							current: 1,
							size: 20,
							budgetCode: ""
						},
						// 分页
						pager: {
							total: 0, // 总页数
							current: 1, // 当前页数
							size: 20 // 每页显示多少条
						},
        }
    },

    computed: {
        ...mapGetters(['permissions']),
        ...mapState({user: state => state.user.user}),
    },

    created() {
        this.lists = getDict('budget_department');
        this.lists2 = getDict('budget_type');
        this.lists3 = getDict('budget_status');
    },

    mounted(){
        this.get_button = this.permissions['budgetadjust_get']
        this.add_button = this.permissions['budgetadjust_add']
        this.edit_button = this.permissions['budgetadjust_edit']
        this.del_button = this.permissions['budgetadjust_del']
				this.getListData();
    },

    methods: {
				getListData (params = {}) {
					this.loading = true;
					this.params.applyDept = this.user.deptId
					let requestData = { ...this.params, ...params };
					fetchList(requestData)
						.then(res => {
							const { current, size, total, records } = res.data.data;
							this.tableData = records;
							this.params = requestData;
							this.pager = { total, current, size };
						})
						.finally(() => {
							this.loading = false;
						});
				},
        /**
         * 根据主键ID删除
         * @param scope
         */
        deleteRow(scope) {
            this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                delObj(scope.row.applyId).then(response => {
                    this.getListData()
                })

                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        /**
         * 跳转到申报页面
         * @param scope
         */
        async toEditData(scope) {
            const resp1 = await getUUID()

            const resp2 = await getUUID()

            this.fileId = resp1.data.msg
            this.fileId2 = resp2.data.msg

            this.$router.push({name: 'adjustapply_add', query: {'applyId': scope.row.applyId,'fileId':resp1.data.msg,'fileId2':resp2.data.msg,'fileId3':scope.row.fileId,'fileId4':scope.row.fileId2} })
        },

        /**
         * 跳转到编辑查看页面
         * @param scope
         */
        toLookData(scope) {
            this.$router.push({name: 'adjust_look', query: {'applyId': scope.row.applyId,'fileId':scope.row.fileId,'fileId2':scope.row.fileId2,'fileId3':scope.row.applyFileId,'fileId4':scope.row.applyFileId2} })
        },

        dictVal_department: function(row, column, cellValue){
            let obj = this.lists.find(p => p.value == cellValue )
            if(obj){
                return obj.label
            }
            return ''
        },

        dictVal_type: function(row, column, cellValue){
            let obj = this.lists2.find(p => p.value == cellValue )
            if(obj){
                return obj.label
            }
            return ''
        },

        dictVal_status: function(row, column, cellValue){
            let obj = this.lists3.find(p => p.value == cellValue )
            if(obj){
                return obj.label
            }
            return ''
        },

    }
}
